package com.zzxx.photograph.service.impl;

import com.zzxx.photograph.dao.*;
import com.zzxx.photograph.dao.impl.*;
import com.zzxx.photograph.domain.*;
import com.zzxx.photograph.service.ImgService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ImgServiceImpl implements ImgService {
    private ImgDao imgDao =new ImgDaoImpl();
    private FavoriteDao favoriteDao = new FavoriteDaoImpl();
    private AuthorDao authorDao = new AuthorDaoImpl();
    private CategoryDao categoryDao=new CategoryDaoImpl();
    private CommentDao commentDao = new CommentDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    @Override
    public PageBean<Img> findImgByIname(String itag, int currentPage, int pageSize) {
        PageBean<Img> pb =new PageBean<>();
        int start = (currentPage - 1) * pageSize;
        int totalCount=imgDao.findCountByIname(itag);
        List<Img> list = imgDao.findByIname(itag, start, pageSize);
        int totalPage=(totalCount+pageSize-1)/pageSize;
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setList(list);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public boolean imgIsFavorite(int iid, int uid) {
        int count = favoriteDao.findCountByIidAndUid(iid,uid);
        return count != 0;
    }
    @Override
    public PageBean<Img> findAll(int currentPage, int pageSize) {
        PageBean<Img> pb =new PageBean<>();
        int start = (currentPage - 1) * pageSize;
        int totalCount=imgDao.findCount();
        List<Img> list= imgDao.findAll(start, pageSize);
        int totalPage=(totalCount+pageSize-1)/pageSize;
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setList(list);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void favorite(int iid, int uid) {
        favoriteDao.insert(iid,uid);
    }

    @Override
    public void cancelFavorite(int iid, int uid) {
        favoriteDao.delete(iid,uid);
    }

    @Override
    public PageBean<Img> findImgsByUid(int uid, int pageSize, int currentPage) {
        PageBean<Img> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int start = (currentPage - 1) * pageSize;
        // 在tab_favorite中通过uid查询iid
        List<Integer> iidList = favoriteDao.findIidByUid(uid);
        // 在img中通过iid查询一个img对象存到list中
        List<Img> list = new ArrayList<>();
        for(int iid : iidList) {
            Img img = imgDao.findImgByRid(iid);
            list.add(img);
        }
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public Img findImgByIid(int iid) {
        Img img = imgDao.findImgByIid(iid);
        Author author = authorDao.findByAid(img.getAid());
        img.setAuthor(author);
        return img;
    }

    @Override
    //浏览量
    public void addClick(int iid) {
        imgDao.addClick(iid);
    }

    @Override
    //收藏次数
    public int findfavoriteCount(int iid) {
        return favoriteDao.favoriteCount(iid);
    }

    @Override
    public String findSrcByIid(int iid) {
        return imgDao.findSrcByIid(iid);
    }

    @Override
    public PageBean<Img> findByFavorite(int currentPage, int pageSize) {
        PageBean<Img> pb =new PageBean<>();
        int start = (currentPage - 1) * pageSize;
        int totalCount=favoriteDao.findCountByIid();
        List<FavoriteNum> _list=favoriteDao.findIid(start, pageSize);
        List<Img> list = new ArrayList<>();
        for(FavoriteNum i :_list){
            Img img = imgDao.findImgByIid(i.getIid());
            img.setFavoriteNum(i.getNum());
            list.add(img);
        }
        int totalPage=(totalCount+pageSize-1)/pageSize;
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setList(list);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public PageBean<Img> findImgsByIname(Map<String, String[]> condition, int currentPage, int pageSize) {
        PageBean<Img> pb =new PageBean<>();
        int start = (currentPage - 1) * pageSize;
        int totalCount=imgDao.findCountsByIname(condition);
        List<Img> list = imgDao.findByInames(condition, start, pageSize);
        int totalPage=(totalCount+pageSize-1)/pageSize;
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setList(list);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void addImg(Img img) {
        imgDao.addImg(img);
        try{
            Author author = authorDao.findByAid(img.getAid());
        } catch (Exception e) {
            authorDao.addAuthor(img.getAuthor());
        }
    }

    @Override
    public PageBean<Img> findImgByAid(int aid, int currentPage, int pageSize) {
        PageBean<Img> pb =new PageBean<>();
        int start = (currentPage - 1) * pageSize;
        int totalCount=imgDao.findCountsByAid(aid);
        List<Img> list = imgDao.findImgByAid(aid, start, pageSize);
        int totalPage=(totalCount+pageSize-1)/pageSize;
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setList(list);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public Integer getMaxId() {
        return imgDao.findMaxId();
    }

    @Override
    public Integer findCid(String cname) {
        return categoryDao.findCidByCname(cname);

    }

    @Override
    public void updateImg(Img img) {
        imgDao.updateImg(img);
    }

    @Override
    public void deleteImg(Integer iid) {
        imgDao.deleteImg(iid);
    }

    @Override
    public void comment(int iid, int uid) {

    }

    @Override
    public List<Comment> loadComment(int iid) {
        return commentDao.loadComment(iid);

    }

    @Override
    public void addComment(int iid, String text, int uid,String time) {
        commentDao.addComment(iid,text,uid,time);
    }

    @Override
    public void deleteComment(int comid) {
        commentDao.deleteComment(comid);
    }

    @Override
    public User findUserbyUid(Integer uid) {
        return userDao.getUserByUid(uid);
    }

    @Override
    public Integer countComment(int iid) {
        return commentDao.countComment(iid);
    }

    @Override
    public int addSupport(int comid) {
        return commentDao.addSupport(comid);
    }

    @Override
    public int addNonSupport(int comid) {
        return commentDao.addNonSupport(comid);
    }

    @Override
    public void deleteFavoriteImg(Integer iid) {
        favoriteDao.deleteFavoriteImg(iid);
    }

}
